package com.lam.common.components.email;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataSource;
import javax.activation.FileTypeMap;

import org.apache.commons.lang3.StringUtils;
import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.NumberUtil;

public class EmailSendInfo {
	
	/** 发件人地址/账号 */
	private String sendUserAddress;
	/** 发件人账号密码 */
	private String sendUserPassword;
	/** 收件人地址，多个地址用英文逗号分隔 */
	private String receiveAddress;
	/** 抄送收件人地址，多个地址用英文逗号分隔 */
	private String receiveAddressCc;
	/** 密送收件人地址，多个地址用英文逗号分隔 */
	private String receiveAddressBcc;
	/** 邮件主题 */
	private String subject;
	/** 邮件正文 */
	private String text;
	/** 邮件正文内容类型 */
	private String textType;
	/** 附件类型 */
	private String mimeType;
	/** 附件 */
	private Map<String, DataSource> attachment = new LinkedHashMap<>();
	
	private String smtpHost;
	private int smtpPort;
	
	private String imapHost;
	private int imapPort;
	
	/** 是否保存到收件箱，需要邮箱支持 */
	private boolean save2SendEmailFolder = false;
	
	/** 出现错误收件地址重试 */
	private boolean invalidAddressesResend = true;
	
	private Properties smtpProperties = new Properties();
	private Properties imapProperties = new Properties();
	
	//默认设置为腾讯邮箱
	public EmailSendInfo() {
		
		setTextType("text/html;charset=UTF-8");
		
		smtpHost = "smtp.exmail.qq.com";
		smtpPort = 465;
		
		imapHost = "smtp.exmail.qq.com";
		imapPort = 143;
		
        // 连接协议 smtp
		smtpProperties.put("mail.transport.protocol", "smtp");
        // 主机名 smtp.exmail.qq.com  qq:smtp.qq.com "smtp.qq.com"
		smtpProperties.put("mail.smtp.host", smtpHost);
        // 端口号
		smtpProperties.put("mail.smtp.port", smtpPort);
		smtpProperties.put("mail.smtp.auth", "true");
        // 设置是否使用ssl安全连接 ---一般都使用
		smtpProperties.put("mail.smtp.ssl.enable", "false");
		
		smtpProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		// 设置是否使用tls
		smtpProperties.put("mail.smtp.starttls.enable", "true");
        // 设置是否显示debug信息 true 会在控制台显示相关信息
		smtpProperties.put("mail.debug", "true");
        
		// 准备连接服务器的会话信息
		imapProperties.put("mail.store.protocol", "imap");
		imapProperties.put("mail.imap.host", imapHost);
		imapProperties.put("mail.imap.port", imapPort);
		imapProperties.put("mail.smtp.auth", "true"); // 需要请求认证
		imapProperties.put("mail.debug", "true");
	}
	
	public static String getMimeType(String fileName){
		return FileTypeMap.getDefaultFileTypeMap().getContentType(fileName);
	}
	
	public boolean hasAttachment(){
		return CollectionUtil.isNotEmpty(attachment);
	}
	
	public String getSendUserAddress() {
		return sendUserAddress;
	}
	public void setSendUserAddress(String sendUserAddress) {
		this.sendUserAddress = sendUserAddress;
		smtpProperties.put("mail.smtp.user", sendUserAddress);
		imapProperties.put("mail.smtp.user", sendUserAddress);
	}
	public String getSendUserPassword() {
		return sendUserPassword;
	}
	public void setSendUserPassword(String sendUserPassword) {
		this.sendUserPassword = sendUserPassword;
		smtpProperties.put("mail.smtp.password", sendUserPassword);
		imapProperties.put("mail.smtp.password", sendUserPassword);
	}
	public String getReceiveAddress() {
		return receiveAddress;
	}
	public void setReceiveAddress(String receiveAddress) {
		this.receiveAddress = receiveAddress;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	
	public Properties getSmtpProperties() {
		return smtpProperties;
	}

	public void setSmtpProperties(Properties smtpProperties) {
		this.smtpProperties = smtpProperties;
	}

	public Properties getImapProperties() {
		return imapProperties;
	}

	public void setImapProperties(Properties imapProperties) {
		this.imapProperties = imapProperties;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public void setSmtpHost(String smtpHost) {
		if(StringUtils.isNotBlank(smtpHost)){
			this.smtpHost = smtpHost;
			smtpProperties.put("mail.smtp.host", smtpHost);
		}
	}

	public int getSmtpPort() {
		return smtpPort;
	}

	public void setSmtpPort(String smtpPort) {
		if(StringUtils.isNotBlank(smtpPort)){
			setSmtpPort(NumberUtil.toInt(smtpPort, this.smtpPort));
		}
	}
	
	public void setSmtpPort(int smtpPort) {
		this.smtpPort = smtpPort;
		smtpProperties.put("mail.smtp.port", smtpPort);
	}

	public String getImapHost() {
		return imapHost;
	}

	public void setImapHost(String imapHost) {
		if(StringUtils.isNotBlank(imapHost)){
			this.imapHost = imapHost;
			imapProperties.put("mail.imap.host", imapHost);
		}
	}

	public int getImapPort() {
		return imapPort;
	}

	public void setImapPort(String imapPort) {
		if(StringUtils.isNotBlank(imapPort)){
			setImapPort(NumberUtil.toInt(imapPort, this.imapPort));
		}
	}
	
	public void setImapPort(int imapPort) {
		this.imapPort = imapPort;
		imapProperties.put("mail.imap.port", imapPort);
	}

	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	public String getTextType() {
		return textType;
	}

	public void setTextType(String textType) {
		this.textType = textType;
	}

	public Map<String, DataSource> getAttachment() {
		return attachment;
	}

	public void addAttachment(String attachmentName, DataSource dataSource) {
		this.attachment.put(attachmentName, dataSource);
	}
	
	public void addAttachmentAll(Map<String, DataSource> attachment) {
		this.attachment.putAll(attachment);
	}

	public String getReceiveAddressCc() {
		return receiveAddressCc;
	}

	public void setReceiveAddressCc(String receiveAddressCc) {
		this.receiveAddressCc = receiveAddressCc;
	}

	public String getReceiveAddressBcc() {
		return receiveAddressBcc;
	}

	public void setReceiveAddressBcc(String receiveAddressBcc) {
		this.receiveAddressBcc = receiveAddressBcc;
	}

	public boolean isSave2SendEmailFolder() {
		return save2SendEmailFolder;
	}

	public void setSave2SendEmailFolder(boolean save2SendEmailFolder) {
		this.save2SendEmailFolder = save2SendEmailFolder;
	}

	public boolean isInvalidAddressesResend() {
		return invalidAddressesResend;
	}

	public void setInvalidAddressesResend(boolean invalidAddressesResend) {
		this.invalidAddressesResend = invalidAddressesResend;
	}

}
